package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.InsureMapper;
import com.gxa.pingan.pojo.Car;
import com.gxa.pingan.pojo.CarBrand;
import com.gxa.pingan.pojo.Insure;
import com.gxa.pingan.service.InsureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2021/10/27  14:26:21
 */
@Service("insureService")
@Transactional(rollbackFor = {Throwable.class}, propagation = Propagation.REQUIRED)
public class InsureServiceImpl implements InsureService {

    @Autowired
    InsureMapper insureMapper;

    /**
     * 投保申请列表查询的方法
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合
     */
    @Override
    public List<Insure> getList(Integer page, Integer size) throws BusinessException {
        if (page != null) {
            if (page <= 0) {
                throw new BusinessException(ExceptionCode.PAGE_ERROR);
            } else {
                page = (page - 1) * size;
            }
        } else {
            page = 0;
        }
        if (size != null) {
            if (size <= 0) {
                throw new BusinessException(ExceptionCode.SIZE_ERROR);
            }
        } else {
            size = 10;
        }
        return insureMapper.getList(page, size);
    }

    /**
     * 投保申请列表计数的方法
     *
     * @return 返回查询到的总数
     */
    @Override
    public Integer getListCount() {
        return insureMapper.getList(null, null).size();
    }
    /*
    /**
     * 品牌型号获取的方法
     *
     * @return 返回是否操作成功
    @Override
    public List<Map<String, String>> getListCar() {
        return insureMapper.getListCar();
    }
     */

    /**
     * 品牌型号获取的方法
     *
     * @return 返回是否操作成功
     */
    @Override
    public List<CarBrand> getListCarBS() {
        return insureMapper.getListCarBS();
    }

/*

    @Override
    public List<CarBrand> getListCarBrand() {
        return insureMapper.getListCarBrand();
    }

    @Override
    public List<CarSeries> getListCarSeries(Long id) {
        return insureMapper.getListCarSeries(id);
    }
*/

    /**
     * 投保申请同意或者拒绝的方法
     *
     * @param id 投保申请的id
     * @return 返回是否操作成功
     */
    @Override
    public Boolean updateIsOk(Boolean isOk, Long id) {
        Integer integer = insureMapper.updateIsOk(isOk, id);
        return integer != 0;
    }

    /*@Override
    public Boolean add(Insure insure, Car car) {
        Integer add = insureMapper.add(insure);
        Integer integer = insureMapper.addCar(car);
        return add != 0 && integer != 0;
    }*/

    /**
     * 添加投保申请的方法
     *
     * @param map 投保申请的各种字段
     * @return 返回是否操作成功
     */
    @Override
    public Boolean add(Map map) {
        Insure insure = new Insure();
        insure.setName(map.get("name").toString());
        insure.setPhone(map.get("phone").toString());
        insure.setCarNum(map.get("carNum").toString());
        insure.setPositiveImg(map.get("positiveImg").toString());
        insure.setNegativeImg(map.get("negativeImg").toString());
        insure.setPhoneCode(map.get("phoneCode").toString());
        insure.setMailbox(map.get("mailbox").toString());
        insure.setVerificationCode(map.get("verificationCode").toString());
        insure.setAppTime(new Timestamp(System.currentTimeMillis()));
        Integer add = insureMapper.add(insure);

        Car car = new Car();
        car.setCarNum(map.get("carNum").toString());
        car.setEngineNum(map.get("engineNum").toString());
        car.setCarCode(map.get("carCode").toString());
        car.setBrandModel(map.get("brandModel").toString());
        car.setBoardDate(Timestamp.valueOf(map.get("boardDate").toString()));
        Integer integer = insureMapper.addCar(car);

        return add != 0 && integer != 0;
    }

    @Override
    public String getPIN() {
        StringBuilder verificationCode = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            verificationCode.append((int) (Math.random() * 9 + 1));
        }
        System.out.println(verificationCode);
        return verificationCode.toString();
    }
}
